#!/usr/bin/env python3
# group: quick
#
# Simple mirror test
#
# Copyright (c) 2018 Virtuozzo International GmbH. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import sys
import os
import iotests
from iotests import qemu_img_create, qemu_io, file_path, log

from qemu.machine import QEMUMachine

iotests.script_initialize(supported_fmts=['qcow2'])

# Note:
# This test was added to check that mirror dead-lock was fixed (see previous
# commit before this test addition).
# And it didn't reproduce if at least one of the following:
# 1. use small image size
# 2. use raw format (not qcow2)
# 3. drop kvm and use iotests.VM() (maybe, because of qtest) (however, it still
#    reproduces, if just drop kvm, but gdb failed to produce full backtraces
#    for me)
# 4. add iothread

size = 1 * 1024 * 1024 * 1024

disk = file_path('disk')

# prepare source image
qemu_img_create('-f', iotests.imgfmt, '-o', 'preallocation=metadata', disk,
                str(size))

vm = QEMUMachine(iotests.qemu_prog)
vm.add_args('-accel', 'kvm', '-accel', 'tcg')
if iotests.qemu_default_machine == 's390-ccw-virtio':
        vm.add_args('-no-shutdown')
vm.add_args('-drive', 'id=src,file=' + disk)
vm.launch()

log(vm.qmp('object-add', qom_type='throttle-group', id='tg0',
           limits={'bps-total': size}))

log(vm.qmp('blockdev-add',
           **{ 'node-name': 'target',
               'driver': 'throttle',
               'throttle-group': 'tg0',
               'file': {
                   'driver': 'null-co',
                   'size': size
                } }))

log(vm.qmp('blockdev-mirror', device='src', target='target', sync='full'))

try:
    vm.event_wait('BLOCK_JOB_READY', timeout=10.0)
except:
    vm.shutdown()
    raise

vm.shutdown()
